package com.spring.cas.config;

import org.jasig.cas.client.session.SingleSignOutFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.cas.web.CasAuthenticationFilter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutFilter;

/**
 * @description:
 * @author: lfc
 * @version: 2020/7/22 14:33
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

   @Autowired
   private AuthenticationProvider authenticationProvider;
   @Autowired
   private AuthenticationEntryPoint authenticationEntryPoint;
   @Autowired
   private SingleSignOutFilter singleSignOutFilter;

   @Autowired
   private LogoutFilter logoutFilter;
   @Autowired
   private CasAuthenticationFilter casAuthenticationFilter;

//   @Override
//   protected void configure(AuthenticationManagerBuilder builder) {
//       builder.authenticationProvider(authenticationProvider);
//   }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/login/cas","favicon.ico","/error","/logout/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .and()
                .addFilter(casAuthenticationFilter)
                .addFilterBefore(singleSignOutFilter, CasAuthenticationFilter.class).addFilterBefore(logoutFilter, LogoutFilter.class);
    }
}
